/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.util;

import java.awt.Component;
import java.awt.FontMetrics;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTreeTable;

/**
 *
 * @author Administrator
 */
public class SwingUtil {

    public static void PreferredJXTableColumn(JXTable table) {
        table.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
        FontMetrics fm = table.getFontMetrics(table.getFont());
        TableColumnModel columnModel = table.getColumnModel();
        int columnIndex;
        for (int index = 0; index < columnModel.getColumnCount(); index++) {
            columnIndex=table.convertColumnIndexToModel(index);
            int rows = table.getRowCount();
            int maxLength = 0;
            if (columnIndex != -1 && rows > 0) {
                if (table.getColumnClass(columnIndex).isAssignableFrom(String.class)&&canResize(table, columnIndex)) {
                    for (int i = 0; i < rows; i++) {
                        Object objValue = table.getValueAt(i, columnIndex);
                        if (objValue instanceof java.lang.String) {
                            int thisLen = fm.stringWidth((String) objValue);
                            maxLength = Math.max(maxLength, thisLen);
                        }
                    }
                    if (maxLength > 0) {
                        table.getColumnModel().getColumn(index).setPreferredWidth(maxLength + 7);
                    }
                }
            }
        }
    }

    private static boolean canResize(JXTable table, int index) {
        TableColumn tblColumn = table.getColumnModel().getColumn(index);
        return (tblColumn != null) && table.getTableHeader().getResizingAllowed() && tblColumn.getResizable();
    }

    private static int getPreferredWidthForCloumn(JXTable table, int icol) {
        TableColumnModel tcl = table.getColumnModel();
        TableColumn col = tcl.getColumn(icol);
        int c = col.getModelIndex(), width = 0, maxw = 0;
        for (int r = 0; r < table.getRowCount(); ++r) {
            TableCellRenderer renderer = table.getCellRenderer(r, c);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, c), false, false, r, c);
            width = comp.getPreferredSize().width;
            maxw = width > maxw ? width : maxw;
        }
        return maxw;
    }

    public static void PreferredJXTreeTableColumn(JXTreeTable treeTable) {
        TableColumnModel model=treeTable.getColumnModel();
        treeTable.setAutoCreateColumnsFromModel(false);
        FontMetrics fm = treeTable.getFontMetrics(treeTable.getFont());
        for (int index = 0; index < model.getColumnCount(); index++) {
            int rows = treeTable.getRowCount();
            int maxLength = 0;
            int columnIndex=index;
            if (index != -1 && rows > 0) {
                if (treeTable.getTreeTableModel().getColumnClass(columnIndex).isAssignableFrom(String.class)) {
                    for (int i = 0; i < rows; i++) {
                        Object objValue = treeTable.getValueAt(i, columnIndex);
                        if (objValue instanceof java.lang.String) {
                            int thisLen = fm.stringWidth((String) objValue);
                           
                            maxLength = Math.max(maxLength, thisLen);
                        }
                    }
                    if (maxLength > 0) {
                        
                        if(index==0)
                            model.getColumn(index).setPreferredWidth(maxLength + 60);
                        else
                            model.getColumn(index).setPreferredWidth(maxLength + 7);
                    }
                }
                treeTable.getTableHeader().setColumnModel(model);
            }
        }
    }

}
